//api路由
var models = require('../db.js');
var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var $sql = require('../sqlMap.js');

// 连接数据库
var conn = mysql.createConnection(models.mysql);
conn.connect();
var jsonWrite = function (res, ret) {
    if (typeof ret === 'undefined') {
        res.json({
            code: '1', msg: '操作失败'
        });
    }
    else {
        res.json(
            ret
        );
    }
};
// 增加注册接口
router.post('/addUser', (req, res) => {
    var sql = $sql.user.add;
    var params = req.body;
    console.log(params);
    conn.query(sql, [params.username, params.password], function (err, result) {
        if (err) {
            console.log(err);
        }
        if (result) {
            jsonWrite(res, result);
        }
    })
});
// 添加登录接口
router.get('/login', (req, res) => {
    var sql = $sql.user.login;
    var params = req.query;
    console.log(params);
    conn.query(sql, [params.username, params.password], function (err, result) {
        if (err) {
            console.log(err);
        }
        if (result) {
            if (result.length > 0) {
                res.json({
                    code: 0,
                    msg: '登录成功',
                    data: result[0]
                });
            } else {
                res.json({
                    code: 1,
                    msg: '登录失败'
                });
            }
        }
    });
});


// 判断是否存在接口
router.get('/checkScore', (req, res) => {
    var sql = $sql.user.checkScore;
    var params = req.query;
    console.log(params);
    conn.query(sql, [params.uid], function (err, result) {
        if (err) {
            console.log(err);
        }
        if (result) {
            if (result.length > 0) {
                res.json({
                    code: 0,
                    msg: '存在',
                    data: result[0]
                });
            } else {
                res.json({
                    code: 1,
                    msg: '未找到'
                });
            }
        }
    });
});

// 添加分数接口
router.post('/totalScore', (req, res) => {
    var sql = $sql.user.totalScore;
    var params = req.body;
    console.log(params);
    conn.query(sql, [params.uid, params.score,params.card,params.cost,params.gametime], function (err, result) {
        if (err) {
            console.log(err);
        }
        if (result) {
            jsonWrite(res, result);
        }
    })
});

// 更新分数接口
router.post('/updateScore', (req, res) => {
    var sql = $sql.user.updateScore;
    var params = req.body;
    console.log(params);
    conn.query(sql, [params.score,params.card,params.cost,params.gametime,params.uid], function (err, result) {
        if (err) {
            console.log(err);
        }
        if (result) {
		jsonWrite(res, result);
		}
        
    })
});


// 排行榜接口
router.get('/getRank', (req, res) => {
    var sql = $sql.user.getRank;
    conn.query(sql,  function (err, result) {
        if (err) {
            console.log(err);
        }
        if (result) {
            jsonWrite(res, result);
        }
    });
});


module.exports = router;
